package com.tozed.widget;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.tozed.test.R;

import android.app.AlertDialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class IPEdit extends LinearLayout {

	private EditText mFirstIP;
	private EditText mSecondIP;
	private EditText mThirdIP;
	private EditText mFourthIP;


	public IPEdit(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.ip_edit, this);
		
		mFirstIP = (EditText)findViewById(R.id.ip_first);
		mSecondIP = (EditText)findViewById(R.id.ip_second);
		mThirdIP = (EditText)findViewById(R.id.ip_third);
		mFourthIP = (EditText)findViewById(R.id.ip_fourth);
		
		mFirstIP.setText("192");
		mSecondIP.setText("168");
		mThirdIP.setText("1");
		mFourthIP.setText("1");

		OperatingEditText(context);
	}

	private void OperatingEditText(final Context context) {
		mFirstIP.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (s.length() > 0) {
					if (Integer.parseInt(s.toString().trim()) > 255) {
						mFirstIP.setText("255");
					}
					
					if (s.length() >= 3) {
						mSecondIP.setFocusable(true);
						mSecondIP.requestFocus();
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

		mSecondIP.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (s.length() > 0) {
					if (Integer.parseInt(s.toString().trim()) > 255) {
						mSecondIP.setText("255");
					}
					
					if (s.length() >= 3) {
						mThirdIP.setFocusable(true);
						mThirdIP.requestFocus();
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

		mThirdIP.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (s.length() > 0) {
					if (Integer.parseInt(s.toString().trim()) > 255) {
						mThirdIP.setText("255");
					}
					
					if (s.length() >= 3) {
						mFourthIP.setFocusable(true);
						mFourthIP.requestFocus();
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

		mFourthIP.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (s.length() > 0) {
					if (Integer.parseInt(s.toString().trim()) > 255) {
						mFourthIP.setText("255");
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	public String getText(Context context) {
		String ip = mFirstIP.getText().toString() 
					+ "." + mSecondIP.getText().toString() 
					+ "." + mThirdIP.getText().toString() 
					+ "." + mFourthIP.getText().toString();
		
		Pattern p = Pattern.compile("^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$");
		Matcher m = p.matcher(ip);
		
		if (!m.find()) {
			AlertDialog ad = new AlertDialog.Builder(context).create();
			TextView tv = new TextView(context);
			tv.setGravity(Gravity.CENTER);
			tv.setTextSize(20);
			tv.setText("\n" + getResources().getString(R.string.str_ip_err) + "\n");
			ad.setView(tv);
			ad.show();
			
			return null;
		}
		
		return ip;
	}
	
	public void setText(String ip) {
		String[] str = ip.split("\\.");
		
		mFirstIP.setText(str[0]);		
		mSecondIP.setText(str[1]);
		mThirdIP.setText(str[2]);
		mFourthIP.setText(str[3]);

	}
}
